﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace projekt_1._0
{

    class BeszallitokDal
    {



        private String CodFiscal;
        private String Cui;
        private String Nev;
        private String Helyseg;
        private String Cim;
        private String Telefon;
        private String BankNev;
        private String BankSzamlaSzam;
        //A DataGridView feltoltese
        public void DgwFill(DataGridView dataGridView1, TextBox KezdoBetuT, SqlConnection cn)
        {

            String TempS;
            TempS = KezdoBetuT.Text.ToString();
            TempS = TempS.ToUpper();
            if (TempS == "")
                TempS = "L";
            if (TempS == "")
            {
                TempS = "I";
            }
            SqlDataAdapter da = new SqlDataAdapter(
                                                    "Select  CegNevB,CodFiscalB, CodUnicDeInregistrareB, HelysegB, CimB, TelefonB, BankNevB, BankSzamlaB " +
                                                    "From Beszallitok " +
                                                     "Where Beszallitok.CegNevB Like \'" + TempS.ToString() + "%\' AND TelefonB > '0'"
                                                     , cn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Alkalmazottak");
            dataGridView1.DataSource = ds.Tables["Alkalmazottak"].DefaultView;

        }
        // a data grid feltolteses cim alapjan
        public void SzuresHelyseg(DataGridView dataGridView1, TextBox tb, SqlConnection cn)
        {
            String H;
            H = tb.Text.ToString();
            if (H == "")
            {
                H = "K";
            }
            SqlDataAdapter da = new SqlDataAdapter(
                                                    "Select  CegNevB,CodFiscalB, CodUnicDeInregistrareB, HelysegB, CimB, TelefonB, BankNevB, BankSzamlaB " +
                                                    "From Beszallitok " +
                                                     "Where Beszallitok.HelysegB Like \'" + H + "%\'"
                                                     , cn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Alkalmazottak");
            dataGridView1.DataSource = ds.Tables["Alkalmazottak"].DefaultView;

        }
        //A Cod Fidscal adattag beallitasa    
        public void BeallitCodFiscal(TextBox tb)
        {
            CodFiscal = tb.Text.ToString();
        }
        //A Cod Cod Unic De Inregistrare adattag beallitasa
        public void BeallitCui(TextBox tb)
        {
            Cui = tb.Text.ToString();
        }
        //A cegnev adattag beallitasa
        public void BeallitCegNev(TextBox tb)
        {
            Nev = tb.Text.ToString();

        }
        //A  helyseg adattag beallitasa
        public void BeallitHelyseg(TextBox tb)
        {
            Helyseg = tb.Text.ToString();
        }
        //A  cim adattag beallitasa
        public void BeallitCim(TextBox tb)
        {
            Cim = tb.Text.ToString();
        }
        //A cim adattag beallitasa
        public void BeallitTelefonb(TextBox tb)
        {
            Telefon = tb.Text.ToString();
        }
        //A banknev adattag beallitasa
        public void BeallitBankNev(TextBox tb)
        {
            BankNev = tb.Text.ToString();
        }
        //A bankszamla adattag beallitasa
        public void BeallitBankSzamal(TextBox tb)
        {
            BankSzamlaSzam = tb.Text.ToString();
        }
        public bool Mentes(SqlConnection cn)
        {
            switch (MessageBox.Show("A kovetkezo adatok lesznek elmentve az Alkalmazottak tablaba: \n Cod Fiscal: " + CodFiscal + "\n CUI: " + Cui + "\n Cegnev: " + Nev + "\n Helyseg: " + Helyseg + "\n Cim: " + Cim + "\n Telefon: " + Telefon + "\n BanlNev: " + BankNev + "\n Bank Szamla szam: " + BankSzamlaSzam,
                         "Mentes?",
                         MessageBoxButtons.YesNo,
                         MessageBoxIcon.Question))
            {
                case DialogResult.Yes:
                    SqlDataAdapter da = new SqlDataAdapter("Insert into Beszallitok " +
                                                           "Values (\'" + CodFiscal + "\',\'" + Cui + "\',\'" + Nev + "\',\'" + Helyseg + "\',\'" + Cim + "\',\'" + Telefon + "\',\'" + BankNev + "\',\'" + BankSzamlaSzam + "\')"
                                                           , cn);
                    SqlDataAdapter da2 = new SqlDataAdapter("Select * From Beszallitok", cn);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Beszallitok");
                    da2.Fill(ds, "Beszallitok");
                    da.Update(ds.Tables["Beszallitok"]);


                    return true;
                    break;
                case DialogResult.No:
                    return false;
                    break;
            }
            return true;
        }
        public bool MentesModositasUtan(SqlConnection cn)
        {
            switch (MessageBox.Show("A kovetkezo adatok lesznek elmentve az Alkalmazottak tablaba: \n Cod Fiscal: " + CodFiscal + "\n CUI: " + Cui + "\n Cegnev: " + Nev + "\n Helyseg: " + Helyseg + "\n Cim: " + Cim + "\n Telefon: " + Telefon + "\n BanlNev: " + BankNev + "\n Bank Szamla szam: " + BankSzamlaSzam,
                         "Mentes?",
                         MessageBoxButtons.YesNo,
                         MessageBoxIcon.Question))
            {
                case DialogResult.Yes:

                    SqlDataAdapter da = new SqlDataAdapter("Update Beszallitok " +
                                                           "Set CodFiscalB = \'" + CodFiscal + "\',CodUnicDeInregistrareB = \'" + Cui + "\',CegNevB = \'" + Nev + "\',HelysegB = \'" + Helyseg + "\',CimB = \'" + Cim + "\',TelefonB = \'" + Telefon + "\',BankNevB = \'" + BankNev + "\',BankSzamlaB = \'" + BankSzamlaSzam + "\'  " +
                                                           "Where CodFiscalB = \'" + CodFiscal + "\' "
                                                           , cn);
                    SqlDataAdapter da2 = new SqlDataAdapter("Select * From Beszallitok", cn);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Beszallitok");
                    da2.Fill(ds, "Beszallitok");
                    da.Update(ds.Tables["Beszallitok"]);



                    break;
                case DialogResult.No:
                    return false;
                    break;
            }
            return true;

        }
        public void FeltoltModositashoz(DataGridView dgw, TextBox tbCodFiscal, TextBox tbCui, TextBox tbNev, TextBox tbHelyseg, TextBox tbCim, TextBox tbTelefon,TextBox tbBankNev, TextBox tbBankSzamla)
        {
            tbCodFiscal.Text = dgw.CurrentRow.Cells[1].Value.ToString();
            tbCui.Text = dgw.CurrentRow.Cells[2].Value.ToString();
            tbNev.Text = dgw.CurrentRow.Cells[0].Value.ToString();
            tbHelyseg.Text = dgw.CurrentRow.Cells[3].Value.ToString();
            tbCim.Text = dgw.CurrentRow.Cells[4].Value.ToString();
            tbTelefon.Text = dgw.CurrentRow.Cells[5].Value.ToString();
            tbBankNev.Text = dgw.CurrentRow.Cells[6].Value.ToString();
            tbBankSzamla.Text = dgw.CurrentRow.Cells[7].Value.ToString();


        }
        public void Torol(DataGridView dgw, SqlConnection cn)
        {
            switch (MessageBox.Show("Biztosan torolni szeretne a kivalasztott sort?",
                        "Torles?",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question))
            {
                case DialogResult.Yes:
                    String CodFiscal2;
                    CodFiscal2 = dgw.CurrentRow.Cells[1].Value.ToString();
                    SqlDataAdapter da = new SqlDataAdapter("Update Beszallitok " +
                                                              "Set TelefonB = '0' " +
                                                              "Where CodFiscalB = \'" + CodFiscal2 + "\' "
                                                              , cn);
                    SqlDataAdapter da2 = new SqlDataAdapter("Select * From Beszallitok", cn);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Beszallitok");
                    da2.Fill(ds, "Beszallitok");
                    da.Update(ds.Tables["Beszallitok"]);
                    break;
                case DialogResult.No:
                    break;
            }


        }
    }
}
